package com.lancoo.answer.view.activity

import android.content.Context
import android.content.Intent
import android.util.Log
import android.view.View
import androidx.databinding.ObservableArrayList
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.gson.Gson
import com.lancoo.answer.R
import com.lancoo.answer.databinding.EvActivityKnowledgeListBinding
import com.lancoo.answer.helper.recyclerview.BindingViewAdapter
import com.lancoo.answer.helper.recyclerview.SingleTypeAdapter
import com.lancoo.answer.model.AnswerConstant
import com.lancoo.answer.model.ConstantBean
import com.lancoo.answer.model.bean.ImportKlg
import com.lancoo.answer.model.eventBean.KlgEventBean
import com.lancoo.answer.util.encodeUtils.TextUtils
import com.lancoo.answer.view.base.BaseVMActivity
import org.greenrobot.eventbus.EventBus

/**
 * Created by Dadong on 2022/1/5
 * Describe:知识点列表
 * 传知识点唯一编码
 */
class EvKnowledgeListActivity : BaseVMActivity<EvActivityKnowledgeListBinding>() {
    private var knowledgeList: ObservableArrayList<ImportKlg> = ObservableArrayList()
    private var typeIndex = 0
    private var quesIndex = 0

    companion object {
        fun loadActivity(mContext: Context, typeIndex: Int, quesIndex: Int, enableTrain: Boolean) {
            val mIntent = Intent(mContext, EvKnowledgeListActivity::class.java)
            mIntent.putExtra(AnswerConstant.KEY_TYPEINDEX, typeIndex)
            mIntent.putExtra(AnswerConstant.KEY_QUESINDEX, quesIndex)
            mIntent.putExtra(AnswerConstant.KEY_TRAIN_STATE, enableTrain)
            mContext.startActivity(mIntent)
        }
    }

    private val mAdapter: SingleTypeAdapter<ImportKlg> by lazy {
        SingleTypeAdapter(this, R.layout.ev_item_knowledge, knowledgeList)
    }

    override fun setLayoutID(): Int {
        return R.layout.ev_activity_knowledge_list
    }

    override fun initView() {
        mBinding.sbtnBack.setOnClickListener {
            finish()
        }
    }

    override fun initData() {
        if ( ConstantBean.constantExamBean == null){
            mBinding.rvKnowledge.visibility = View.GONE
            mBinding.groupEmpty.visibility = View.VISIBLE
            return
        }
        if (ConstantBean.constantExamBean!!.TypeList ==null){
            mBinding.rvKnowledge.visibility = View.GONE
            mBinding.groupEmpty.visibility = View.VISIBLE
            return
        }
        val kindBeanList = ConstantBean.constantExamBean!!.TypeList
        typeIndex = intent.getIntExtra(AnswerConstant.KEY_TYPEINDEX, 0)
        quesIndex = intent.getIntExtra(AnswerConstant.KEY_QUESINDEX, 0)
        knowledgeList.addAll(kindBeanList!![typeIndex].QuesList!![quesIndex].ImportKlgList as ArrayList)
        if (knowledgeList.isNullOrEmpty()) {
            mBinding.rvKnowledge.visibility = View.GONE
            mBinding.groupEmpty.visibility = View.VISIBLE
        } else {
            mBinding.groupEmpty.visibility = View.GONE
            initAdapter()
        }
    }

    fun initAdapter() {
        mBinding.rvKnowledge.adapter = mAdapter.apply {
            itemPresenter = ItemPresenter()
        }
        val linearLayoutManager = LinearLayoutManager(this)
        mBinding.rvKnowledge.layoutManager = linearLayoutManager.apply {
            orientation = LinearLayoutManager.VERTICAL
        }
        mAdapter.notifyDataSetChanged()
    }

    inner class ItemPresenter : BindingViewAdapter.ItemClickPresenter<ImportKlg> {
        override fun onItemClick(v: View, item: ImportKlg) {
            //todo 跳转知识点详情
            Log.e("6666", Gson().toJson(item))
            val bean = KlgEventBean()
            bean.klgCode = item.UniqueCode
            if (TextUtils.isEmpty(bean.klgCode)) {
                bean.klgCode = item.Code
            }
            bean.klgName = item.Name
            bean.isEnableTrain = intent.getBooleanExtra(AnswerConstant.KEY_TRAIN_STATE, true)
            EventBus.getDefault().post(bean)
        }
    }
}